@rem Description: guestAgent build script, Platform:ARM64
@echo off

set CURRENT_DIR=%cd%
set TOP=%CURRENT_DIR%\..\..
set OPENSOURCE=%TOP%\open_source
set PROTOBUF_X64_DIR=%TOP%\open_source\protobuf_x64
set PROTOBUF_ARM64_DIR=%TOP%\open_source\protobuf
set SPDLOG_DIR=%TOP%\open_source\spdlog
set DETOURS_DIR=%TOP%\open_source\Detours
set OUTPUT_DIR=%TOP%\output
set LIB_DIR=%TOP%\service\vm_agent\agent_service\lib
set WIN_EVT_MONITOR_LIB_DIR=%TOP%\service\vm_agent\win_evt_monitor\lib
set PROCESS_DLL_LIB_DIR=%TOP%\service\vm_agent\process_monitor\process_monitor\lib
set SVGA_DLL_LIB_DIR=%TOP%\service\vm_agent\svga_adapter\svga_adapter\lib
set DLL_INCLUDE_DIR=%TOP%\service\vm_agent\process_monitor\process_monitor\include
set PROCESS_MONITOR_DIR=%TOP%\service\vm_agent\process_monitor
set SVGA_ADAPTER_DIR=%TOP%\service\vm_agent\svga_adapter

rem change the following path to your local vcvarsamd64_arm64.bat path while compiling vmagent at localhost
set VCVARSAMD64_ARM64_CMD="C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Auxiliary\Build\vcvarsamd64_arm64.bat"
set VCVARSAMD64_X64_CMD="C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Auxiliary\Build\vcvars64.bat"

echo executing prebuild_download.bat...
call prebuild_download.bat

rem build protobuf-4.25.2
echo "building protobuf ARM64..."
cd %PROTOBUF_ARM64_DIR%
cmake -Dprotobuf_BUILD_TESTS=OFF -DCMAKE_CXX_STANDARD=17 -G "Visual Studio 16 2019" -A ARM64 .
cmake --build . --config Release

del /f /s /q %LIB_DIR%\libprotobuf.lib
xcopy Release\libprotobuf.lib %LIB_DIR%\

rem build protobuf-4.25.2
echo "building protobuf x64..."
cd %PROTOBUF_X64_DIR%
cmake -Dprotobuf_BUILD_TESTS=OFF -DCMAKE_CXX_STANDARD=17 -G "Visual Studio 16 2019" -A x64 .
cmake --build . --config Release

echo "begin to create protobuf cc and header file"
mkdir %OUTPUT_DIR%
.\Release\protoc.exe --proto_path=%TOP%\common\connect\proto --cpp_out=%OUTPUT_DIR% guest2host.proto
.\Release\protoc.exe --proto_path=%TOP%\common\connect\proto --cpp_out=%OUTPUT_DIR% host2guest.proto

rem build spdlog
cd %SPDLOG_DIR%
mkdir build
cd build
cmake -G "Visual Studio 16 2019" -A ARM64 ..
cmake --build . --config Release

del /f /s /q %LIB_DIR%\spdlog.lib
xcopy Release\spdlog.lib %LIB_DIR%\

del /f /s /q %WIN_EVT_MONITOR_LIB_DIR%\spdlog.lib
xcopy Release\spdlog.lib %WIN_EVT_MONITOR_LIB_DIR%\

rem build detours
cd %DETOURS_DIR%
%comspec% /c "%VCVARSAMD64_ARM64_CMD% && nmake -f Makefile"
%comspec% /c "%VCVARSAMD64_X64_CMD% && nmake -f Makefile"
del /f /s /q %PROCESS_DLL_LIB_DIR%\detours.lib
del /f /s /q %SVGA_DLL_LIB_DIR%\detours.lib
del /f /s /q %SVGA_DLL_LIB_DIR%_x64\detours.lib
del /f /s /q %PROCESS_DLL_LIB_DIR%\syelog.lib
xcopy lib.ARM64\detours.lib %PROCESS_DLL_LIB_DIR%\
xcopy lib.ARM64\syelog.lib %PROCESS_DLL_LIB_DIR%\
xcopy lib.ARM64\detours.lib %SVGA_DLL_LIB_DIR%\
xcopy lib.X64\detours.lib %SVGA_DLL_LIB_DIR%_x64\
del /f /s /q %DLL_INCLUDE_DIR%\detours.h
del /f /s /q %DLL_INCLUDE_DIR%\detver.h
del /f /s /q %DLL_INCLUDE_DIR%\syelog.h
xcopy include\detours.h %DLL_INCLUDE_DIR%\
xcopy include\detver.h %DLL_INCLUDE_DIR%\
xcopy include\syelog.h %DLL_INCLUDE_DIR%\

rem build process_monitor
cd %PROCESS_MONITOR_DIR%
msbuild process_monitor_injector.sln /p:Platform="ARM64" /p:Configuration=release

echo "Copy process monitor to output directory."
del /f /s /q %OUTPUT_DIR%\*.dll
xcopy ARM64\Release\process_monitor.dll %OUTPUT_DIR%\

rem build svga_adapter
cd %SVGA_ADAPTER_DIR%
msbuild svga_adapter.sln /p:Platform="ARM64" /p:Configuration=release
msbuild svga_adapter.sln /p:Platform="x64" /p:Configuration=release

echo "Copy svga_adapter to output directory."
del /f /s /q %OUTPUT_DIR%\svga_adapter.dll
del /f /s /q %OUTPUT_DIR%\svga_adapter_x64.dll
xcopy ARM64\Release\svga_adapter.dll %OUTPUT_DIR%\
xcopy x64\Release\svga_adapter_x64.dll %OUTPUT_DIR%\

rem build app filter agent
cd %TOP%\app_filter
call build.bat
rem build agent
cd %CURRENT_DIR%
msbuild vm_agent.sln /p:Platform="ARM64" /p:Configuration=release

rem build updateBar
msbuild update\update_bar\updateBar.vcxproj /t:clean
msbuild update\update_bar\updateBar.vcxproj /p:Platform="ARM64" /p:Configuration=release
echo "Copy VMAgent.exe to output directory."
del /f /s /q %OUTPUT_DIR%\*.exe
del /f /s /q %OUTPUT_DIR%\*.pdb
xcopy update\update_bar\ARM64\Release\*.exe %OUTPUT_DIR%\
xcopy update\update_bar\ARM64\Release\*.pdb %OUTPUT_DIR%\

xcopy ARM64\Release\*.exe %OUTPUT_DIR%\
xcopy ARM64\Release\*.pdb %OUTPUT_DIR%\
xcopy x64\Release\*.exe %OUTPUT_DIR%\
xcopy x64\Release\*.dll %OUTPUT_DIR%\
xcopy x64\Release\*.pdb %OUTPUT_DIR%\
xcopy %TOP%\app_filter\filter_agent\build\Release\FilterAgent.exe %OUTPUT_DIR%\

rmdir /s /q %CURRENT_DIR%\ARM64
rmdir /s /q %CURRENT_DIR%\agent_service\lib
rmdir /s /q %CURRENT_DIR%\agent_service\ARM64
rmdir /s /q %CURRENT_DIR%\performance\PreloadHelper\ARM64
rmdir /s /q %CURRENT_DIR%\performance\VisualEffectsEdit\ARM64
rmdir /s /q %CURRENT_DIR%\ps_hide_window\x64
rmdir /s /q %CURRENT_DIR%\ps_process_control\x64
rmdir /s /q %CURRENT_DIR%\application_window_helper\ARM64
rmdir /s /q %CURRENT_DIR%\display\ARM64
rmdir /s /q %CURRENT_DIR%\pre_process_wndcontrol\ARM64
rmdir /s /q %CURRENT_DIR%\process_monitor\ARM64
rmdir /s /q %CURRENT_DIR%\process_monitor\process_monitor\ARM64
rmdir /s /q %CURRENT_DIR%\process_monitor\process_monitor\lib
rmdir /s /q %CURRENT_DIR%\svga_adapter\ARM64
rmdir /s /q %CURRENT_DIR%\svga_adapter\X64
rmdir /s /q %CURRENT_DIR%\svga_adapter\svga_adapter\ARM64
rmdir /s /q %CURRENT_DIR%\svga_adapter\svga_adapter\x64
rmdir /s /q %CURRENT_DIR%\wndtext_fetch_helper\ARM64
rmdir /s /q %CURRENT_DIR%\update\update_bar\ARM64
rmdir /s /q %CURRENT_DIR%\update\update_bar\Debug
rmdir /s /q %CURRENT_DIR%\update\update_confim\ARM64
rmdir /s /q %CURRENT_DIR%\win_evt_monitor\ARM64
rmdir /s /q %CURRENT_DIR%\win_evt_monitor\lib

if %ERRORLEVEL% EQU 0 ( goto end ) else ( goto error )

:end
echo "clean up."
del /f /s /q %OUTPUT_DIR%\guest2host.pb.h
del /f /s /q %OUTPUT_DIR%\guest2host.pb.cc
del /f /s /q %OUTPUT_DIR%\host2guest.pb.h
del /f /s /q %OUTPUT_DIR%\host2guest.pb.cc
echo "compile success."
exit /b 0

:error
echo "compile failed!"
exit /b 1
